/*
 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

package cn.ccimage.passwordkeeper;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.widget.Toast;


/**
 * Settings activity for AppRTC.
 */
public class SettingsActivity extends Activity implements OnSharedPreferenceChangeListener {
    private SettingsFragment settingsFragment;

    private String keyprefCurrentPwd;
    private String keyprefCurrentTime;
    private String keyprefHistoryPwd;
    private String keyprefHistoryTime;
    private String keyprefGlobalPwd;
    private String keyprefGlobalPwdCheck;
    private String keyprefPasswordLength;
    private String keyprefEnableFingerprint;
    private String kStringBlank = "";

    private boolean kPwdChanged = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GetPreferenceKey();
        // Display the fragment as the main content.
        settingsFragment = new SettingsFragment();
        getFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, settingsFragment)
                .commit();


    }

    @Override
    protected void onResume() {
        super.onResume();
        // Set summary to be the user-description for the selected value
        SharedPreferences sharedPreferences =
                settingsFragment.getPreferenceScreen().getSharedPreferences();
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);

        updateSummary(sharedPreferences, keyprefPasswordLength);
        //updateSummary(sharedPreferences, keyprefGlobalPwd);
        //updateSummary(sharedPreferences, keyprefGlobalPwdCheck);
        updateSummaryB(sharedPreferences, keyprefEnableFingerprint);
    }

    @Override
    protected void onPause() {
        super.onPause();
        SharedPreferences sharedPreferences =
                settingsFragment.getPreferenceScreen().getSharedPreferences();
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals(keyprefGlobalPwd)) {
            kPwdChanged = true;
        }
        // clang-format off
        if (key.equals(keyprefPasswordLength)) {
            updateSummary(sharedPreferences, key);
        }
        else if(key.equals(keyprefEnableFingerprint)){
            updateSummaryB(sharedPreferences, key);
        }
    }

    private void updateSummary(SharedPreferences sharedPreferences, String key) {
        Preference updatedPref = settingsFragment.findPreference(key);
        if(null == updatedPref) {
            return;
        }
        // Set summary to be the user-description for the selected value
        updatedPref.setSummary(sharedPreferences.getString(key, ""));
    }

    private void updateSummaryB(SharedPreferences sharedPreferences, String key) {
        Preference updatedPref = settingsFragment.findPreference(key);
        if(null == updatedPref) {
            return;
        }
        // Set summary to be the user-description for the selected value
        boolean value = sharedPreferences.getBoolean(key, false);
        String valueStr =  value ? "true":"false";
        updatedPref.setSummary(valueStr);
    }

    @Override
    public void onBackPressed() {
        SharedPreferences sharedPreferences =
                settingsFragment.getPreferenceScreen().getSharedPreferences();
        String pwd = sharedPreferences.getString(keyprefGlobalPwd, "");
        String pwdcheck = sharedPreferences.getString(keyprefGlobalPwdCheck, "");

        if(pwd.isEmpty() || pwd.length() <= 0) {
            Toast.makeText(this, R.string.info_text_create_global_pwd, Toast.LENGTH_SHORT).show();
        }
        else if(pwd.length() < 6) {
            Toast.makeText(this, R.string.error_invalid_password, Toast.LENGTH_SHORT).show();
        }
        else if(!pwd.equals(pwdcheck)) {
            Toast.makeText(this, R.string.info_text_check_global_pwd, Toast.LENGTH_SHORT).show();
        }
        else{
            Intent intent=getIntent();
            int result = kPwdChanged ? RESULT_OK : RESULT_CANCELED;
            setResult(result, intent);
            super.onBackPressed();
        }
    }

    private void GetPreferenceKey(){
        //keyprefCurrentPwd = getString(R.string.preference_key_current_pwd);
        //keyprefCurrentTime = getString(R.string.preference_key_current_time);
        //keyprefHistoryPwd = getString(R.string.preference_key_history_pwd);
        //keyprefHistoryTime = getString(R.string.preference_key_history_time);
        keyprefGlobalPwd = getString(R.string.preference_key_global_pwd);
        keyprefGlobalPwdCheck = getString(R.string.preference_key_global_pwd_check);
        keyprefPasswordLength = getString(R.string.preference_key_pwd_length);
        keyprefEnableFingerprint = getString(R.string.preference_key_enable_fingerprint);
    }
}
